<?xml version = '1.0' encoding = 'UTF-8'?>
<HBRRepo><variables><variable name="OQP_Scenario" type="member" usage="const" id="2" product="Planning"><property name="application">SalesPln</property><property name="dimensionInputMode">type</property><property name="dimensionType">Scenario</property><property name="prompt_text">LABEL_QTP_SLS_PLAN_PRMPT_SCENARIO</property><property name="scope">ruleset</property><property name="seeded">true</property><value/></variable><variable name="OQP_Version" type="member" usage="const" id="1" product="Planning"><property name="application">SalesPln</property><property name="dimensionInputMode">type</property><property name="dimensionType">Version</property><property name="prompt_text">LABEL_QTP_SLS_PLAN_PRMPT_VERSION</property><property name="scope">ruleset</property><property name="seeded">true</property><value/></variable><variable name="OQP_Year" type="member" usage="const" id="3" product="Planning"><property name="application">SalesPln</property><property name="dimension">Years</property><property name="dimensionInputMode">name</property><property name="prompt_text">LABEL_QTP_SLS_PLAN_PRMPT_YEAR</property><property name="scope">ruleset</property><property name="seeded">true</property><value/></variable></variables><rulesets/><rules><rule id="1" name="OQP_Topdown Padding" product="Planning"><property name="application">SalesPln</property><property name="plantype">OEP_QTP</property><property name="seeded">true</property><variable_references><variable_reference name="OQP_Scenario" id="2"><property name="application">SalesPln</property><property name="hidden">false</property><property name="rule_name">OQP_Topdown Padding</property><property name="seq">1</property><property name="type">3</property><property name="useAsOverrideValue">false</property><property name="validation_value">OEP_Plan</property></variable_reference><variable_reference name="OQP_Version" id="1"><property name="application">SalesPln</property><property name="hidden">false</property><property name="rule_name">OQP_Topdown Padding</property><property name="seq">2</property><property name="type">3</property><property name="useAsOverrideValue">false</property><property name="validation_value">OEP_Working</property></variable_reference><variable_reference name="OQP_Year" id="3"><property name="application">SalesPln</property><property name="hidden">false</property><property name="rule_name">OQP_Topdown Padding</property><property name="seq">3</property><property name="type">3</property><property name="useAsOverrideValue">false</property><property name="validation_value">FY17</property></variable_reference></variable_references><script type="groovy">				/*RTPS:{OQP_Version} {OQP_Scenario} {OQP_Year} */
                /*PURPOSE: This rule applies padding percentage to all territories underneath main territory or resouce dimension*/
				String entity; 
				def grid=operation.grid;
				boolean isDataModified=false;
                boolean isUnitsModel=false;
                String driverDimMember;
				List&lt;String> CustomRelDimensions=[];
				List&lt;String> CustomMembers=[];
				List&lt;String> RowDimensions=[]
				List&lt;String> CustomDimensions=[];
                int iCnt;
				String strCrossDim = "";
				try
				{
                /*Evalute if Quota planning model is enabled for units or  Amount */
				 Application app=operation.getApplication();
				 Cube qtp=operation.getCube(); 
				Dimension dim=app.getDimension(DimensionType.ACCOUNT,qtp);
				if (dim.hasMember("OQP_Units",qtp)==true)
				{
					isUnitsModel=true;
					driverDimMember="OQP_Last Year Units";
				}
				else
				{
					isUnitsModel=false;
					driverDimMember="OQP_Last Year Revenue";                    
				}
				String Scenario=rtps.OQP_Scenario.member.name;
				String Version=rtps.OQP_Version.member.name;				
				String Year=rtps.OQP_Year.member.name;	
                PeriodDimension perioddim=app.getDimension(DimensionType.PERIOD,qtp).asType(PeriodDimension);                              
                String LastPeriod=perioddim.getLastPeriod().getName()
              	println(LastPeriod);
                /*Identify POV Dimensoins and their members*/
				List&lt;DataGrid.HeaderCell> hcells=grid.getPov()
					hcells.each{
						def dimName = it.getDimName();
						String mbrName=it.getMbrName();
						if (!mbrName.equals(Scenario) &amp;&amp; !mbrName.equals(Version) &amp;&amp; !it.getDimensionType().equals(DimensionType.SIMPLE_CURRENCY) &amp;&amp; !mbrName.equals(Year))
						{
							CustomMembers.add('''"'''+mbrName+'''"''');
							CustomRelDimensions.add('''@Relative("'''+mbrName+'''",0)''');
							strCrossDim= strCrossDim + "\""+ mbrName+"\"->"
							CustomDimensions.add('''"'''+dimName+'''"''');
						}					
					}	
               	/*START : Generate Calculation Script  - Main Fix*/   
				StringBuilder cscript = new StringBuilder();
				cscript.append('''SET AGGMISSG ON;\n''');
				cscript.append('''SET UPDATECALC OFF;\n''');
				cscript.append('''FIX(@Relative("yeartotal",0),'''+ Year +''',''')
				cscript.append ('''"'''+Version+'''"''')
				cscript.append (''',"'''+Scenario+'''"''')
				if (app.getCurrencyMode()!=CurrencyMode.SINGLE_CURRENCY)
				{
				String sCurrency =  operation.grid.pov.find { it.dimName == 'Currency' }.essbaseMbrName 
                //cscript.append (''',"'''+app.getDefaultCurrency()+'''"''')
                cscript.append (''',"'''+ sCurrency +'''"''')
				}
				cscript.append (''')\n''')
                /*Identify Row Dimensions*/
				List&lt;DataGrid.Row> rows = grid.getRows()
				for (DataGrid.Row row : rows) 
				{
					List&lt;DataGrid.HeaderCell> subRows = row.getHeaders();
					for(DataGrid.HeaderCell cell : subRows) 
					  {
						String memName = cell.getDimName();  
					  if (!RowDimensions.contains(memName))
					   {
							RowDimensions.add(memName)      	 
					   }     	 
					 }        
				}
				for(String dimName:RowDimensions)
				{
					def ls=operation.grid.dataCellIterator.collect([] as Set,{ DataCell cell ->
					cell.getMemberName(dimName)
					})
					if (ls.size()>0)
						{
							isDataModified=true;
							entity=ls[0];
						}
				 }
               
                /*Allocate Padded Adjustment Quota to descendents of Territory or Resource*/ 
                cscript.append ('''		FIX(@Relative("'''+entity+'''",0)''');
				if (CustomRelDimensions.size()>0)
				{ 
                	cscript.append(''','''+CustomRelDimensions.join(''','''))                 
				}                
                cscript.append (''')\n''')				
				cscript.append('''			"OQP_Padding Adjustment Value"=''')
                cscript.append(strCrossDim +'''"'''+entity+'''"->"OQP_Input Padding%"->"YearTotal" *"OQP_Adjusted Target Quota";\n''')
				cscript.append ('''		ENDFIX\n''')
				/*Aggregate Padded Quota*/
				cscript.append ('''	FIX("OQP_Padding Adjustment Value")\n''')
				if (CustomDimensions.size()>0)
				{
				  for (iCnt=0;iCnt&lt;CustomDimensions.size()-1;iCnt++)
                   {
                    	cscript.append('''			FIX(@RELATIVE('''+CustomDimensions[iCnt]+''',0))\n''')              
                   }                
                  for (iCnt=CustomDimensions.size()-1;iCnt>0;iCnt--)
                	{
                		cscript.append('''				AGG('''+CustomDimensions[iCnt]+''');\n''')                        
                		cscript.append('''			ENDFIX\n''')
                	}
                	cscript.append('''			AGG('''+CustomDimensions[0]+''');\n''')
				}
				cscript.append ('''		@IDESCENDANTS("'''+entity+'''");\n''');
				cscript.append ('''	ENDFIX\n''')
                /*Recalcualate padding percentage at the top level members*/
				cscript.append('''	FIX(@IDESCENDANTS("'''+ entity+'''")''')
                if (CustomRelDimensions.size()>0)
                {
                	cscript.append(''','''+CustomMembers.join(''','''))
                }
                cscript.append(''')\n''')
                cscript.append('''		"OQP_Padding%"(\n''');
				cscript.append('''			"OQP_Padding%"->"'''+LastPeriod+'''"=("OQP_Padded Quota"->"YearTotal"-"OQP_Adjusted Target Quota"->"YearTotal")/"OQP_Adjusted Target Quota"->"YearTotal";\n''')
                cscript.append('''		);\n''');
				cscript.append ('''	ENDFIX\n''')
				cscript.append ('''ENDFIX\n''')
				println(cscript.toString())
				return cscript.toString();				
				}catch(all) {
				throw new HspRuntimeException('Error occured while executing business rule');
			    } 								
		
		</script></rule></rules><components/><deployobjects><deployobject product="2" application="salespln" plantype="oep_qtp" obj_id="1" obj_type="1" name="OQP_TOPDOWN PADDING"/></deployobjects></HBRRepo>